package jehc.cloud.gen.service.generator;

import jehc.cloud.common.util.ExceptionUtil;
import jehc.cloud.gen.model.XtGenerator;
import jehc.cloud.gen.model.XtGeneratorTableColumn;
import jehc.cloud.gen.model.XtGeneratorTableColumnManyToOne;
import jehc.cloud.gen.model.XtGeneratorTableManyToOne;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;

/**
 * @Desc
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
public class Gutil {
	/**
	 * 总生成代码
	 * @param xtGeneratorTableColumnList
	 * @param xtGenerator
	 * @return
	 */
    public int createCode(List<XtGeneratorTableColumn> xtGeneratorTableColumnList, XtGenerator xtGenerator){
    	GeneratorModel generatorModel = new GeneratorModel();
    	GeneratorDao generatorDao = new GeneratorDao();
    	GeneratorService generatorService = new GeneratorService();
    	GeneratorWeb generatorWeb = new GeneratorWeb();
    	int i = 0;
    	try {
    		///////////////////////////////单表及主表生成开始/////////////////////////
    		//设置为当前为主表
			xtGenerator.setIs_main_table(true);
    		//1实体层
    		generatorModel.createModel(xtGeneratorTableColumnList, xtGenerator);
    		//2数据层
    		generatorDao.createDaoAll(xtGeneratorTableColumnList, xtGenerator);
    		//3业务层
    		generatorService.createServiceAll(xtGeneratorTableColumnList, xtGenerator);
    		//4控制层
    		generatorWeb.createWeb(xtGeneratorTableColumnList, xtGenerator);
			//5页面层（Bootstrap开发模式）
			GeneratorBootPageList generatorBootPageList = new GeneratorBootPageList();
			GeneratorBootPageAdd generatorBootPageAdd = new GeneratorBootPageAdd();
			GeneratorBootPageUpdate generatorBootPageUpdate = new GeneratorBootPageUpdate();
			GeneratorBootPageDetail generatorBootPageDetail = new GeneratorBootPageDetail();
			generatorBootPageList.createPageAll(xtGeneratorTableColumnList, xtGenerator);
			generatorBootPageAdd.createPageAll(xtGeneratorTableColumnList, xtGenerator);
			generatorBootPageUpdate.createPageAll(xtGeneratorTableColumnList, xtGenerator);
			generatorBootPageDetail.createPageAll(xtGeneratorTableColumnList, xtGenerator);
    		///////////////////////////////单表及主表生成结束/////////////////////////
    		//6一对多子表生成代码
    		commonManyToOne(xtGenerator);
			i = 1;
		} catch (Exception e) {
			i = 0;
			e.printStackTrace();
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
    	return i;
    }

    /**
     * 一对多子表生成
     * @param xtGenerator
     */
    private void commonManyToOne(XtGenerator xtGenerator){
    	//设置为非主表
		xtGenerator.setIs_main_table(false);
    	GeneratorModel generatorModel = new GeneratorModel();
    	GeneratorDao generatorDao = new GeneratorDao();
    	GeneratorService generatorService = new GeneratorService();
    	GeneratorWeb generatorWeb = new GeneratorWeb();
    	//如果是一对多则对子表生成代码直到控制层（Model，Dao，Service，Web层需要生成）
		if(xtGenerator.getIs_one_to_many().equals("1")){
			List<XtGeneratorTableManyToOne> xtGeneratorTableManyToOnes = xtGenerator.getXt_Generator_TableMany_To_OneList();
			for(int j = 0; j < xtGeneratorTableManyToOnes.size(); j++){
				XtGeneratorTableManyToOne xtGeneratorTableManyToOne = xtGeneratorTableManyToOnes.get(j);
				List<XtGeneratorTableColumnManyToOne> xtGeneratorTableColumnManyToOneList = xtGeneratorTableManyToOne.getXt_Generator_Table_ColumnMany_To_OneList();
				List<XtGeneratorTableColumn> xtGeneratorTableColumnList = new ArrayList<XtGeneratorTableColumn>();
				for(int h = 0; h < xtGeneratorTableColumnManyToOneList.size(); h++){
					XtGeneratorTableColumnManyToOne xtGeneratorTableColumnManyToOne = xtGeneratorTableColumnManyToOneList.get(h);
					XtGeneratorTableColumn xtGeneratorTableColumn = new XtGeneratorTableColumn();
					xtGeneratorTableColumn.setCHARACTER_MAXIMUM_LENGTH(xtGeneratorTableColumnManyToOne.getCHARACTER_MAXIMUM_LENGTH());
					xtGeneratorTableColumn.setCOLUMN_COMMENT(xtGeneratorTableColumnManyToOne.getCOLUMN_COMMENT());
					xtGeneratorTableColumn.setCOLUMN_KEY(xtGeneratorTableColumnManyToOne.getCOLUMN_KEY());
					xtGeneratorTableColumn.setColumn_label_anchor(xtGeneratorTableColumnManyToOne.getColumn_label_anchor());
					xtGeneratorTableColumn.setColumn_label_position(xtGeneratorTableColumnManyToOne.getColumn_label_position());
					xtGeneratorTableColumn.setCOLUMN_NAME(xtGeneratorTableColumnManyToOne.getCOLUMN_NAME());
					xtGeneratorTableColumn.setColumn_type(xtGeneratorTableColumnManyToOne.getColumn_type());
					xtGeneratorTableColumn.setDATA_TYPE(xtGeneratorTableColumnManyToOne.getDATA_TYPE());
					xtGeneratorTableColumn.setIS_NULLABLE(xtGeneratorTableColumnManyToOne.getIS_NULLABLE());
					xtGeneratorTableColumn.setIsHidden(xtGeneratorTableColumnManyToOne.getIsHidden());
					xtGeneratorTableColumn.setXt_script_id(xtGeneratorTableColumnManyToOne.getXt_script_id());
					xtGeneratorTableColumnList.add(xtGeneratorTableColumn);
				}
				xtGenerator.setIs_main_table(false);
				xtGenerator.setIs_one_to_many("1");
				xtGenerator.setFk(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_fkey());
				xtGenerator.setXt_generator_web_package(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_web_package());
				xtGenerator.setXt_generator_model_package(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_model_package());
				xtGenerator.setXt_generator_service_package(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_service_package());
				xtGenerator.setXt_generator_dao_package(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_dao_package());
				xtGenerator.setXt_generator_tbname(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name());
				xtGenerator.setXt_generator_tbcomment(xtGeneratorTableManyToOne.getXt_generator_one_to_many_table_name_zh());
				//1实体层
        		generatorModel.createModel(xtGeneratorTableColumnList, xtGenerator);
        		//2数据层
        		generatorDao.createDaoAll(xtGeneratorTableColumnList, xtGenerator);
        		//3业务层
        		generatorService.createServiceAll(xtGeneratorTableColumnList, xtGenerator);
        		//4控制层
        		generatorWeb.createWeb(xtGeneratorTableColumnList, xtGenerator);
			}
		}
    }
}
